public class MyLinkList implements IList{
    static class LinkedNode{
        public int value;
        public LinkedNode next;
        public LinkedNode prev;

        public LinkedNode(int value) {
            this.value = value;
        }
    }

    public LinkedNode head;
    public LinkedNode last;
    @Override
    public void addFirst(int data) {
        LinkedNode node = new LinkedNode(data);
        if(head == null) {
            head = node;
        }else {
            LinkedNode cur = head;
            node.next = cur;
            cur.prev = node;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        LinkedNode node = new LinkedNode(data);
        if(head == null) {
            head = node;
            last = node;
        }else {
            LinkedNode cur = head;
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
    public LinkedNode prevIndex(int index) {
        LinkedNode cur = head;
        while((index - 1) != 0) {
            index--;
            cur = cur.next;
            if(cur == null || cur.next == null) {
                return null;
            }
        }
        return cur;
    }
    @Override
    public void addIndex(int index, int data) {
        if(index < 0) {
            throw new RuntimeException("下标异常: " + index);
        }
        LinkedNode node = new LinkedNode(data);
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        LinkedNode curPrev = prevIndex(index);
        if(curPrev == null) {
            throw new RuntimeException("下标异常: " + index);
        }
        node.next = curPrev.next;
        curPrev.next = node;
        node.prev = curPrev;
        curPrev.next.prev = node;

    }

    @Override
    public boolean contains(int key) {
        LinkedNode cur = head;
        while(cur != null) {
           if(cur.value == key) {
               return true;
           }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
//        if(head.value == key) {
//            //头删
//            //只有一个元素，且是要删的元素
//            head = head.next;
//            if(head != null) {
//                head.prev = null;
//            }
//        }else if(last.value == key) {
//            //尾删
//            last = last.prev;
//            last.next = null;
//        }else {
//            LinkedNode del = head;
//            while(del != null) {
//                if(del.value == key) {
//                    del.prev.next = del.next;
//                    del.next.prev = del.prev;
//                    return;
//                }
//                del = del.next;
//            }
//            if(del == null) {
//                System.out.println("没有要删除的元数！！");
//            }
//        }
        LinkedNode del = head;
        while(del != null) {
            if(head.value == key) {
                //头删，且只有一个元素
                head = head.next;
                //不止一个元素
                if(head != null) {
                    head.prev = null;
                }
                return;
            }else if(last.value == key) {
                //尾删
                last = last.prev;
                last.next = null;
                return;
            }else {
                if(del.value == key) {
                    del.prev.next = del.next;
                    del.next.prev = del.prev;
                    return;
                }
            }
            del = del.next;
        }
        if(del == null) {
            System.out.println("没有你要删除的元素！！");
        }
    }

    @Override
    public void removeAllKey(int key) {
        LinkedNode del = head;
        while(del != null) {
            if(head.value == key) {
                //头删，且只有一个元素
                head = head.next;
                //不止一个元素
                if(head != null) {
                    head.prev = null;
                }
            }else if(last.value == key) {
                //尾删
                last = last.prev;
                last.next = null;
            }else {
                if(del.value == key) {
                    del.prev.next = del.next;
                    del.next.prev = del.prev;
                }
            }
            del = del.next;
        }
    }

    @Override
    public int size() {
        if(head == null) {
            return 0;
        }
        int count = 0;
        LinkedNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        LinkedNode cur = head;
        while(cur != null) {
            LinkedNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        //可以直接写下面两行代码也可以直接回收该空间
        head = null;
        last = null;
    }

    @Override
    public void display() {
        LinkedNode cur = head;
        while(cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
